<template>
    <div>
        <h1 style="margin-bottom: 30px;margin-left: 70px">资源管理</h1>
        <el-tree
                :data="AllSources"
                show-checkbox
                node-key="id"
                highlight-current
                :check-strictly=status
                :default-checked-keys="Sources"
                :props="defaultProps"
                @check-change="getChecked"
                ref="tree" style="margin-left: 50px;margin-top: 20px">
        </el-tree>
        <el-button type="primary" size="small" style="margin-left: 270px;margin-top: 20px" @click="updateSourceBtn">确定</el-button>
    </div>
</template>
<script>
    import HisAuthRule from '../../model/ww/HisAuthRule'
    const hisAuthRules = HisAuthRule.getInstance();

    export default {
        name: "authority",
        props:['id'],
        data(){
            return {
                AllSources:[],  //所有资源
                Sources:[],   //角色拥有的资源
                defaultProps: { //所有资源
                    children: 'children',
                    label: 'label'
                },
                id2:this.id,
                status:true,
                role :{},     //存角色的资源
                roles:[],
             }
        },
        methods: {
            manageSource() {//查看所有父级子级资源
                hisAuthRules.list().then((resposne) => {
                    this.AllSources = resposne.data.data
                    this.AllSources.forEach(i=>{
                        i.label=i.title
                        if(i.label=="系统设置" && this.id2==1){
                               i.disabled=true
                        }
                        i.children.forEach(ii=>{
                            if(ii.pid==i.id && i.label=="系统设置" ){
                                ii.disabled=true
                            }
                            ii.label=ii.title
                        })
                    })
                })
            },
            showBtn() {  //查看权限所对应的资源
                hisAuthRules.listSource(this.id2).then((response)=>{
                    this.Sources=[]
                    var aa=[]
                    response.data.forEach(i=>{
                        aa.push(i.id)
                        i.children.forEach(ii=>{
                            aa.push(ii.id)
                        })
                    })
                    this.Sources=aa

                })

            },
            updateSourceBtn(){ //更新资源
                console.log(this.id2)
                this.role.id=this.id2
                this.role.rules=this.roles.join(",")
                hisAuthRules.updateSource(this.role).then(()=>{
                    this.$emit("close",123111)
                })
            },
            getChecked() {
                this.status=false
                const authorityIds = this.$refs.tree.getHalfCheckedKeys()
                const childrenIds = this.$refs.tree.getCheckedKeys()
                authorityIds.forEach(i=>{
                    childrenIds.push(i)
                })
                this.roles=childrenIds
            },
        },
        created() {
            this.manageSource();
            this.showBtn()
        },
    }
</script>

<style scoped>

</style>